<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['diy']=$this->plugin_setting('diy');
if($_RQ['op']=='main'){
	$return['params']=array('status'=>100);
	exi($return);
}
/*******op*******/
if($_RQ['op']=='list'){
	//查询进入该页面的客户所有订单
	$params=array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']);
	$return['params']=array();
	if(isset($_RQ['status']) && $_RQ['status']!=100){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}else{
		$return['params']['status']=100;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$order_list=pdo_getslice('jy_tour_order',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	if($order_list){		
		$merch=pdo_get('jy_tour_merch',array('id'=>$item['merch_id'],'uniacid'=>$_SESSION['uniacid']));
		foreach($order_list as &$item){
			if($item['spot_id']){
				$spot=pdo_get('jy_tour_spot',array('id'=>$item['spot_id'],'uniacid'=>$_SESSION['uniacid']));
				$item['type']=1;
				$images=json_decode($spot['images'],true);
				$item['image']=$images['0'];
				$item['address']=$spot['destination_address'];
				
			}else{
				$item['type']=2;
				$way=pdo_get('jy_tour_way',array('id'=>$item['way_id'],'uniacid'=>$_SESSION['uniacid']));
				$images=json_decode($way['images'],true);
				$item['image']=$images['0'];
				$item['address']=$way['destination_address'];
			}

			$ticket=pdo_get('jy_tour_spot_ticket',array('id'=>$item['ticket_id'],'uniacid'=>$_SESSION['uniacid']));
			$item['createtime']=date('Y-m-d H:i:s', $item['createtime']);
			$item['spot']=$spot;
			$item['way']=$way;
			$item['ticket']=$ticket;
			$item['merch']=$merch;
			$item['can_refund']=$this->model('refund')->check_order_rerefund($item['id']);
			$item['statustitle']=$this->order_status[$item['status']];
		}
	}
	$return['list']=$order_list;
	set_html('title','订单列表');
	exi($return);
}
//订单详情

if($_RQ['op']=='detail'){
	if(!$_RQ['id']){
	exi('订单不存在，请重新选择','error');
	}
	$order=pdo_get('jy_tour_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$spot=pdo_get('jy_tour_spot',array('id'=>$order['spot_id'],'uniacid'=>$_SESSION['uniacid']));
	$merch=pdo_get('jy_tour_merch',array('id'=>$order['merch_id'],'uniacid'=>$_SESSION['uniacid']));
	$way=pdo_get('jy_tour_way',array('id'=>$order['way_id'],'uniacid'=>$_SESSION['uniacid']));
	$ticket=pdo_get('jy_tour_spot_ticket',array('id'=>$order['ticket_id'],'uniacid'=>$_SESSION['uniacid']));
	$order['createtime']=date('Y-m-d H:i:s', $order['createtime']);
	$order['ticket']=$ticket;
	$order['way']=$way;
	$order['merch']=$merch;
	$order['spot']=$spot;
	//用户信息
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']));
	$return['order']=$order;
	$return['member']=$member;
	set_html('title','订单详情');
	exi($return);
}

if($_RQ['op']=='del'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_delete('jy_tour_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('删除预约成功');
}
if($_RQ['op']=='pay'){
	$post=get_postdata();
	$id=$post['id'];
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}
	$pay_id=pdo_getcolumn('jy_tour_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id),'pay_id');
	exi($pay_id);
}
if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','list');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','list');
	}
	$order=pdo_get('jy_tour_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	$order['package']=json_decode($order['package'],true);
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['package']['package_price']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	pdo_update('jy_tour_order',array('status'=>'1','pay'=>$payresult['fee']),array('id'=>$order['id']));
	exi('支付成功','success','jy_tour/order/list/status=1');
}